home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / pct2.zip / CHAP2.DOC < prev    next >
Text File  |  1990-06-21  |  11KB  |  310 lines

  1.  
  2.  
  3.  
  4.                                                                             11
  5.  
  6.                                    CHAPTER 2 - DATA
  7.  
  8.  
  9.              Before you start using data, you need to know what data looks
  10.              like. It is not necessary for the data to have a name. For
  11.              instance, the following definition is perfectly legal:
  12.  
  13.                  db   "Mary had a little lamb." 
  14.  
  15.              Unfortunately, the assembler has no way to find it. The normal
  16.              thing is to start the line with a name, and then give the
  17.              definition of the data. The assembler processes the data line by
  18.              line, so a definition on one line does not carry over to another
  19.              line. We can have:
  20.  
  21.                  poem      db   "Mary had a little lamb,"
  22.  
  23.              Notice that names for data don't have colons after them. What if
  24.              we wanted to continue the poem? It isn't going to fit all on one
  25.              line. No problem. All we need to do is define the following lines
  26.              without a name.
  27.  
  28.                  poem      db   "Mary had a little lamb,"
  29.                            db   "It's fleas were white as snow,"
  30.                            db   "And everywhere that Mary went,"
  31.                            db   "She scratched and scratched and scratched."
  32.  
  33.              The assembler still can't find lines 2-4, but starting at the
  34.              first byte of "poem", it can go all the way through the poem one
  35.              byte after the other. By the way, there are no carriage returns
  36.              in the poem right now. They will come later.
  37.  
  38.              So we have the name part, the db part, and the data part. What is
  39.              that db anyway. It stands for Define Byte. Whenever you give the
  40.              name "poem" to the assembler, it knows that you want to deal with
  41.              the data one byte at a time. If you try working a word at a time,
  42.              you will get an assembler error. The legal definitions are:
  43.  
  44.                  DB   define byte         [ 1 byte ]
  45.                  DW   define word         [ 2 bytes ]
  46.                  DD   define doubleword   [ 2X2 bytes = 4 bytes ]
  47.                  DQ   define quadword     [ 4X2 bytes = 8 bytes ]
  48.                  DT   define ten-byte     [ 10 bytes ]
  49.                  DF   define farword      [ 6 bytes - used for 80386 only ] 
  50.  
  51.              Every time you use one of these directives, the assembler
  52.              allocates the number of bytes in brackets for EACH variable. For
  53.              instance in:
  54.  
  55.                  db        "Mary had a little lamb,"
  56.  
  57.              each character inside the quotes is a variable. That's 23
  58.              variables X 1 byte = 23 bytes. In:
  59.  
  60.              ______________________
  61.  
  62.              The PC Assembler Tutor - Copyright (C) 1989 Chuck Nelson
  63.  
  64.  
  65.  
  66.  
  67.              The PC Assembler Tutor                                         12
  68.              ______________________
  69.  
  70.  
  71.                  dq        0, 1, 2, 3, 4
  72.  
  73.              each number is a variable. 5 variables X 8 bytes = 40 bytes.
  74.              Notice from these examples that you can have more than one
  75.              variable on a line but they all share the same defining type.
  76.              What do you do if you have an uninitialized variable, i.e. you
  77.              don't know its starting value? Easy as pie. Here's a four byte
  78.              variable:
  79.  
  80.                  some_data      dd   ?
  81.  
  82.              The question mark lets the assembler know that you didn't forget
  83.              the number but rather you didn't know the number.
  84.  
  85.              The commas are separators. When you write a comma, the assembler
  86.              expects another piece of data on the line. If it doesn't get the
  87.              number, it is an error. That means there can be no commas inside
  88.              a number. 
  89.  
  90.                  dw   32,421
  91.  
  92.              is two variables: 32 and 421.
  93.  
  94.              What if you want to make an array? The assembler has a directive
  95.              for that too:
  96.  
  97.                  dw   150  dup ( 400 )
  98.  
  99.              The 'dup' is for duplicate. This makes 150 two byte copies and
  100.              puts the number 400 in each one.
  101.  
  102.                  db   273  dup ( 'c' )
  103.  
  104.              This makes 273 one byte copies and puts the letter 'c' in each
  105.              one.
  106.  
  107.                  dd   459  dup ( 1, 2, 3, 4, 5 )
  108.  
  109.              This makes 459 copies of what is inside the parentheses. That
  110.              means  ( 5 variables X 4 bytes ) X 459 for a total of 9180 bytes.
  111.              Starting from the beginning of the array, we will have the
  112.              sequence: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3,...
  113.  
  114.                  dq   20000 dup ( 455 ) 
  115.  
  116.              This makes 20000 eight byte copies and causes an assembler error
  117.              because there is a limit of 65,536 bytes for the data and you
  118.              have used 160,000 bytes (20,000 X 8).
  119.  
  120.                  db   7  dup ( 'Mary had a little lamb,')
  121.  
  122.              This makes 7 copies of 'Mary had ..' which is 23 bytes, for a
  123.              total of 161 bytes.
  124.  
  125.                  dw   39 dup ( 28 dup ( 0 ) )
  126.  
  127.  
  128.  
  129.  
  130.  
  131.              Chapter 2 - Data                                               13
  132.              ________________
  133.  
  134.              The assembler even supports nesting, so you can make a
  135.              multi-dimensional array. This is a 39 X 28 array initialized to
  136.              zero. 39 copies of 28 two byte numbers is 2184 bytes.
  137.  
  138.              The standard form for arrays is (1) first define the data type,
  139.              (2) then say how long the array is followed by the keyword "dup"
  140.              and (3) put the initial value inside the parentheses. What if you
  141.              don't know the initial value? Simple:
  142.  
  143.                  dw   347  dup ( ? ) 
  144.  
  145.              The question mark lets the assembler know that you don't know.
  146.  
  147.  
  148.              DEFINING NUMBERS
  149.  
  150.              What kinds of data can you have?
  151.  
  152.              1. A single character inside single or double quotes:
  153.                       'a' , "&" , '|'
  154.  
  155.              2. A string inside single or double quotes:
  156.  
  157.                  "Mary had a little lamb,"
  158.                  'Mary had a little lamb,'
  159.  
  160.              Each character is stored as a byte, and the bytes are stored
  161.              consecutively. If the array starts at address 2743, 2743 = 'M',
  162.              2744 = 'a', 2745 = 'r', 2746 = 'y', 2747 = ' ', etc. As usual in
  163.              these instances, if you want a double quote inside a double
  164.              quoted string or a single quote inside a single quoted string,
  165.              you need to use a pair:
  166.  
  167.                  "Mary asked her fleas ""Why don't you join the circus?"""
  168.                  'Mary asked her fleas "Why don''t you join the circus?"'
  169.  
  170.              3. A decimal number. Decimal is the default:
  171.  
  172.                  27, 44, 641, 89
  173.  
  174.              4. A hex number. A hex number must start with a number, so if the
  175.              highest digit is A - F, there must be a 0 in front.{1} b77h is
  176.              illegal, 0b77h is legal. All hex numbers must be followed by an
  177.              'h':
  178.  
  179.                  0a162H , 0329H , 0DDDh , 7h
  180.  
  181.              5. An octal (base 8) number. An octal is followed either by the
  182.              ____________________
  183.  
  184.                   1 When the assembler looks at something it needs to know
  185.              whether it is a name or a number. Is 'A7' a name or a hex number?
  186.              Is '3D' a name or a number? To solve this problem, all assemblers
  187.              and all compilers insist that -> if the first character is a
  188.              number, it's a number; if the first character is not a number, it
  189.              is not a number. That is why you can't start a variable name with
  190.              a number.
  191.  
  192.  
  193.  
  194.  
  195.              The PC Assembler Tutor                                         14
  196.              ______________________
  197.  
  198.              letter q or the letter o:
  199.  
  200.                  641q , 2345o , 1472o
  201.  
  202.              6. A binary number. A binary number is followed by a b:
  203.  
  204.                  0100100b , 1b , 01001000111010b
  205.  
  206.  
  207.  
  208.              Any of these types can be mixed on a line. For instance:
  209.  
  210.                  db   "Mary had a little lamb," , 13 , 10
  211.  
  212.              13 followed by 10 is CRLF, the PC signal for a carriage return. A
  213.              string in the C language ends with the number 0. If we wanted a C
  214.              string with CRLF, we would have:
  215.  
  216.                  db   "Mary had a little lamb," , 13 , 10 , 0
  217.  
  218.              Another mixed example:
  219.  
  220.                  dw   7 , 010010b , 0FFFFh , 037q
  221.  
  222.              is dopey but legal.
  223.  
  224.  
  225.              You can also have an equation, as long it resolves to a number.
  226.              This calculation is done by the assembler, so the values of
  227.              variables are not allowed:
  228.  
  229.                       dw   ( ( 19 * 7 * 25 ) + 6 ) / ( 9 + 7 )
  230.  
  231.              is legal, but:
  232.  
  233.              data1    dw   25
  234.              data2    dw   7
  235.                       dw   ( ( 19 * 7 * data1 ) + 6 ) / ( 9 + data2 )
  236.  
  237.              is illegal. Everything must be a constant. Remember that when the
  238.              assembler starts calculating it might truncate the partial
  239.              answers, so don't get too fancy.
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.              Chapter 2 - Data                                               15
  260.              ________________
  261.  
  262.                                           SUMMARY
  263.  
  264.              The assembler works one line at a time. Each line with data must
  265.              start with a data type declaration (after an optional name.)
  266.  
  267.                                         DATA TYPES
  268.  
  269.                  DB   define byte         ( 1 byte )
  270.                  DW   define word         ( 2 bytes )
  271.                  DD   define doubleword   ( 2X2 bytes = 4 bytes )
  272.                  DQ   define quadword     ( 4X2 bytes = 8 bytes )
  273.                  DT   define ten-byte     ( 10 bytes )
  274.                  DF   define farword      ( 6 bytes - used for 80386 only ) 
  275.  
  276.  
  277.                                    COMMON INTEGER TYPES
  278.  
  279.                  TYPE                MAX SIGNED               MAX UNSIGNED
  280.  
  281.                  byte                -128/+127                255
  282.                  word              -32768/+32767              65535
  283.                  doubleword     -2147483648/+2147483647       4294967295
  284.  
  285.                  Note that the max. negative integer is 1 larger than the
  286.                  max. positive integer.
  287.  
  288.  
  289.                                POSSIBLE BASES FOR CONSTANTS
  290.  
  291.                  b         binary data
  292.                  o,q       octal data
  293.                  d         decimal data  (default)
  294.                  h         hex data  (must start with a number 0 - 9)
  295.  
  296.  
  297.                                      ARRAY DEFINITIONS
  298.  
  299.                  d*        num1      dup ( data1 )
  300.  
  301.              Using the d* data type (db, dw, dd, dq, etc.) make num1 copies of
  302.              data1 (data1 may be either a single piece of data or a group of
  303.              data.)
  304.  
  305.                                  MULTIPLE DATA ON ONE LINE
  306.  
  307.              Different data elements on the same line are separated by commas.
  308.              All elements on the same line have the same data type.
  309.  
  310.